// components/region/region.js
import { region } from '../../data/region.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //序号 默认北京   拿到省市区的id后查找各自的序号，然后传进来
    multiIndex: {
      type: Array,
      value: [0,0,0],
      observer: function(newval){
        //如果选了地址
        if (this.data.hasaddress){
            let obj = this.data.multiArray
            //重置市和区的数据
            this.setData({
              provinceindex: newval[0],
              cityindex: newval[1]
            })
            obj[1] = region[this.data.provinceindex].childs.map(x => x.name)
            obj[2] = region[this.data.provinceindex].childs[this.data.cityindex].childs.map(x => x.name)
            this.setData({
              city: obj[1],
              county: obj[2],
              multiArray: obj
            })
        }
      }
    },
    //是否已经选择地址
    hasaddress: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    //序列号
    provinceindex:0,
    cityindex:0,
    //总数据
    region:[],
    //省份数组
    province:[],
    //市数组
    city: [],
    //区数组
    county:[],
    //总范围数据
    multiArray: [[], [], []],
    //ID，传给后台使用
    idBox:[0,0,0],
    //当前选择省份
    curpro: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindMultiPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      console.log(this.data.province[e.detail.value[0]], this.data.city[e.detail.value[1]], this.data.county[e.detail.value[2]])

      let idBox = this.data.idBox
      let region = this.data.region
      idBox[0] = region[e.detail.value[0]].id
      idBox[1] = region[e.detail.value[0]].childs[e.detail.value[1]].id
      idBox[2] = region[e.detail.value[0]].childs[e.detail.value[1]].childs[e.detail.value[2]].id
      this.setData({
        multiIndex: e.detail.value,
        idBox: idBox,
        hasaddress:true
      })
      //把选择的地址id发送出去
      this.triggerEvent('addressid', this.data.idBox,)
    },
    bindMultiPickerColumnChange (e) {
      let column = e.detail.column
      let value = e.detail.value
      let curname = this.data.multiArray[column][value] 
      console.log(this.data.multiArray[column])
      console.log('列:' + column)
      console.log('行:' + value)
      console.log(curname)
      let region = this.data.region
      let province = this.data.province
      let city = this.data.city
      let county = this.data.county
      let multiIndex = this.data.multiIndex
      if (column == 0){
        this.setData({
          curpro: curname,
          multiIndex: [value,0,0]
        })
      } else if (column == 1){
        multiIndex[1] = value
        multiIndex[2] = 0
        this.setData({
          multiIndex: multiIndex
        })
      } else if (column == 2){
        multiIndex[2] = value
        this.setData({
          multiIndex: multiIndex
        })
      }
      city = region.filter(x => x.name == this.data.curpro)[0].childs.map(x => x.name)
      county = region.filter(x => x.name == this.data.curpro)[0].childs[column == 0 ? 0 : this.data.multiIndex[1]].childs.map(x => x.name)
      let obj = this.data.multiArray
      obj[1] = city
      obj[2] = county
      this.setData({
        multiArray: obj,
        province: province,
        city: city,
        county: county
      })
    }
  },
  ready () {
    //初始化  默认北京显示
    let province = region.map(x => x.name)
    let city = region[this.data.provinceindex].childs.map(x => x.name)
    let county = region[this.data.provinceindex].childs[this.data.cityindex].childs.map(x => x.name)
    console.log(region)
    this.setData({
      province: province,
      city: city,
      county: county
    })
    let obj = this.data.multiArray
    obj[0] = this.data.province
    obj[1] = this.data.city
    obj[2] = this.data.county
    this.setData({
      multiArray: obj,
      region: region
    })
    console.log(obj)
  }
})
